import OSS from 'ali-oss'
import { Message } from 'element-ui'

// 检查合法性并返回真值
export function checkValid(attr, msg) {
  if (attr == null || attr.length === 0) {
    Message({
      message: msg || 'Error',
      type: 'error',
      duration: 5 * 1000
    })
    return false
  } else return true
}

// 上传文件到OSS
export async function uploadFile(key, file, config) {
  const client = new OSS(config)
  const headers = { 'Access-Control-Allow-Origin': '*' }
  return client.put(key, file, headers)
}

// 删除文件
export async function deleteFile(key, config) {
  const client = new OSS(config)
  return client.delete(key)
}

export function get_suffix(filename) {
  const pos = filename.lastIndexOf('.')
  let suffix = ''
  if (pos !== -1) suffix = filename.substring(pos)
  return suffix
}

// mqtt断开连接
export function disConnect(client) {
  client.end()
  console.log('停止接收数据')
}
